import java.util.ArrayList;
import java.util.LinkedList;

import java.util.*;

import models.EEntity;
import models.Organization;
import models.RoleInOrganization;
import models.User;
import models.Topic;
import models.Role;
import models.RoleInEntity;

import org.junit.Before;
import org.junit.Test;

import antlr.collections.List;

import play.test.Fixtures;
import play.test.UnitTest;

public class shadytest extends UnitTest {

	@Before
	public void setup() {
		Fixtures.deleteAll();
	}

	@Test
	public void test() {

		// TESTING DATA
		
		// Create Organizations
		Organization organization = new Organization("Intel", "LOL", 1).save();

		Organization organization2 = new Organization("HP", "LOL", 1).save();

		// Create new entities
		EEntity entity = new EEntity("LOL",
				"cs and engineering department met lol",
				(Organization) Organization.find("byName", "Intel").first())
				.save();

		EEntity entity2 = new EEntity("IET",
				"Communication and Engineering department",
				(Organization) Organization.find("byName", "Intel").first())
				.save();

		EEntity entity3 = new EEntity("MECHA",
				"mechatronics and engineering department",
				(Organization) Organization.find("byName", "Intel").first())
				.save();

		EEntity entity4 = new EEntity("MET", "cs and engineering department",
				(Organization) Organization.find("byName", "Intel").first())
				.save();


		// Create new User
		User user = new User("Shady", "M", "chadobli", "bluh", new Date(),
				"asdasd@hotmail.com", "adress").save();

		// Create new User
		User user2 = new User("Ahmed", "M", "7amada", "bluh", new Date(),
				"asdasd@hotmail.com", "adress").save();

		// Create new User
		User user3 = new User("chadchi", "M", "shosho", "bluh", new Date(),
				"asas@hoy", "address").save();

		// add "user3" as an organization lead to "organization"
		Role role2 = new Role("OrganizationLead").save();
		RoleInOrganization roro = new RoleInOrganization(organization, user3,
				role2).save();

		// add "user2" as an idea developer to "organization"
		Role role3 = new Role("IdeaDeveloper").save();
		RoleInOrganization roleInOrg = new RoleInOrganization(organization,
				user2, role3).save();

		// add "user" as an organizer in entity "Entity"
		Role role = new Role("Organizer").save();
		RoleInEntity roleInEntity = new RoleInEntity(entity, user, role).save();

		// new topic
		Topic topic = new Topic("Shady topic", 0, "bluh bluh", new Date(),
				entity, user).save();

		// assign a tag with user as the one responsible (should work user is an
		// organizer!)
		Topic.assignTag(user.id, entity.id, topic.id, "YARAB!");

		Topic.assignTag(user.id, entity.id, topic.id, "Hello!");

		assertEquals(topic.tags.get(0).name, "YARAB!");

		assertEquals(topic.tags.get(1).name, "Hello!");

		assertEquals(topic.tags.size(), 2);

		// assign a tag with user2 who is not an organizer (shouldn't work)

		Topic.assignTag(user2.id, entity.id, topic.id, "Hello again!");

		assertEquals(topic.tags.size(), 2);

		// checking handling of duplicate tags

		Topic.assignTag(user.id, entity.id, topic.id, "YARAB!");

		assertEquals(topic.tags.size(), 2);

		// rename the tag YARAB! to chadobli

		Topic.renameTag(user.id, entity.id, topic.id, topic.tags.get(0).id,
				"chadobli");

		assertEquals(topic.tags.get(0).name, "chadobli");

		// remove the renamed tag "chadobli"

		Topic.unassignTag(user.id, entity.id, topic.id, topic.tags.get(0).id);

		assertEquals(topic.tags.size(), 1);
		assertEquals(topic.tags.get(0).name, "Hello!");

		// Testing requestJoin

		Organization.requestJoin(user.id, organization.id);
		Organization.requestJoin(user2.id, organization2.id);

		assertEquals(organization.requestToJoinUsers.size(), 1);
		assertEquals(organization2.requestToJoinUsers.size(), 1);

		assertEquals((long) organization.requestToJoinUsers.get(0).id,
				(long) user.id);
		assertEquals((long) organization2.requestToJoinUsers.get(0).id,
				(long) user2.id);

		// test approveJoin

		Organization.approveJoin(user3.id, organization.id, user.id);

		assertEquals(organization.requestToJoinUsers.size(), 0);

		// Checking isIdeaDeveloper method
		
	
		
		//add user as idea developer in entity "entity"
		User user4 = new User("Shadoo!", "M", "chadobliiiii", "bluh", new Date(),
							 "asdasd@hotmail.com", "adress").save();
		
		
		Role role5 = new Role("IdeaDeveloper").save();
		RoleInEntity RoleInEntity2 = new RoleInEntity(entity,user4,role5).save();
		
		assertEquals(EEntity.isIdeaDeveloper(entity.id,user4.id),true);
		
		assertEquals(EEntity.isIdeaDeveloper(entity.id,user.id),false);
		
		//testing getIdeaDevelopers() methods
		assertEquals(organization.getIdeaDevelopers().contains(user2),true);
		
		//testing the remove self from organziation as idea dev.
		
		Organization.removeSelfFromOrganization(user4.id, organization.id);
		
		//no longer an idea dev
		assertEquals(EEntity.isIdeaDeveloper(entity.id,user4.id),false);
		
		//add user as idea developer in enitity "entity"
		User user5 = new User("shadhh!", "M", "chaddsdobliiiii", "blusdfh", new Date(),
							  "asdasd@hotmail.com", "adress").save();
		
		
		Role role6 = new Role("IdeaDeveloper").save();
		RoleInEntity RoleInEntity3 = new RoleInEntity(entity,user5,role5).save();
		
		//check he is an idea dev
		assertEquals(EEntity.isIdeaDeveloper(entity.id,user5.id),true);
		
		//wont work because user2 is not an organization lead
		Organization.removeMemberFromOrganization(user2.id, user5.id, organization.id);
		
		// he is still in the organization
		assertEquals(EEntity.isIdeaDeveloper(entity.id,user5.id),true);
		
		//this should work because user3 is an organization lead
		Organization.removeMemberFromOrganization(user3.id, user5.id, organization.id);
		
		//worked
		assertEquals(EEntity.isIdeaDeveloper(entity.id,user5.id),false);
		
		
		
	
	}

}
